/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imclient;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import org.w3c.dom.Document;
/**
 *
 * @author Andrew
 */
public class XMLReader {
    
    
    //create a  hashmap object 
    HashMap <String, String> xmlData;
    
    /*
     * 
     *Method to create hashmap of XML file. This method creates a new document builder to parse over the xml file. 
     * @arguments String: name of xml file
     * @returns Hashmap
     *  
     * 
     * 
     */
     
    public HashMap<String, String> readXML(String filename) throws ParserConfigurationException, SAXException, IOException
    {
        //initialize the hashmap object
        xmlData = new HashMap<String, String>();
        //Documentbuilderfactory object created
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //Java API says that a DocumentBuilder object must be created from an existing DocumentBuilderFactory object and calling the newDocumentBuilder() method on said object. 
        DocumentBuilder dBuilder = dbf.newDocumentBuilder();
        
        dbf.setValidating(true); 
        dbf.setIgnoringComments(true);
        
        //Create new file based upon the filename passed to the method
        // Currently, this object is created from simply a filename, no other arguements are passed to the constructor. 
        File file = new File(filename);
        //Read the XML file and create a new xml file
        Document xmlFile = dBuilder.parse(filename);
         //read the DOM and create new hashmap object                 
        readData(xmlFile);
        
        return xmlData;
    }
    
    
    /*
     * Method to recurse over all elements in XML file
     * 
     */
    private void readData(Node node) {
        // base case if this node has no children
        if (!node.hasChildNodes()) {
            //assign key and value 
            String value = node.getNodeValue();
            String key = node.getParentNode().getNodeName();

            //adding them to the hashmap
           xmlData.put(key, value);
        } else {
           
            NodeList childNodes = node.getChildNodes();

            //get each child item
            for (int i = 0; i < childNodes.getLength(); i++)
            {
                
                Node childNode = childNodes.item(i);
                
                //call readData recursively
                readData(childNode);}
        }
    }
}
    /*
    public HashMap<String, String> readXml(String filename) throws Exception {
        DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
        builder.setValidating(true);
        //ignore irrelevant whitespace
        builder.isIgnoringElementContentWhitespace();
        builder.setIgnoringComments(true);
        DocumentBuilder docBuilder = builder.newDocumentBuilder();
        
        
        File xmlFile = new File("config", filename);
        Document doc = docBuilder.parse(xmlFile);
        readProperties(doc);
        
        return xmlData;
    
    }

    private void readProperties(Node node) {
        
        if(!node.hasChildNodes())
        {
            String value = node.getNodeValue();
            String key = node.getParentNode().getNodeName();
            
            xmlData.put(key,  value);
        }else
        {
            NodeList childNodes = node.getChildNodes();
            
            //iterate over the child nodes
            for (int i = 0; i < childNodes.getLength(); i++)
            {
                Node childNode = childNodes.item(i);
                
                //make method recursive
                readProperties(childNode);
                
            }
        }
        
    }
    public void testXML() throws SAXException, IOException{
    File fXmlFile = new File("src/imclient/Config.xml");
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
       
	Document doc = dBuilder.parse(fXmlFile);
 
	//optional, but recommended
	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
	doc.getDocumentElement().normalize();
 
	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
 
	NodeList nList = doc.getElementsByTagName("staff");
 
	System.out.println("----------------------------");
  } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
        */
    
     
    
    


